#!/bin/sh
if [ "$(uci -q get openmptcprouter.settings.version)" = "" ]; then
	if [ "$(uci -q get sqm.wan1)" = "" ]; then
		wan1="$(uci -q get network.wan1.device)"
		wan2="$(uci -q get network.wan2.device)"
		uci -q batch <<-EOF >/dev/null
			delete sqm.eth1
			set sqm.wan1=queue
			set sqm.wan1.qdisc="fq_codel"
			set sqm.wan1.script="simple.qos"
			set sqm.wan1.qdisc_advanced='0'
			set sqm.wan1.linklayer='none'
			set sqm.wan1.enabled='1'
			set sqm.wan1.interface="${wan1}"
			set sqm.wan1.download='0'
			set sqm.wan1.upload='0'
			set sqm.wan1.debug_logging='0'
			set sqm.wan1.verbosity='5'
			set sqm.wan1.iqdisc_opts='autorate-ingress dual-dsthost'
			set sqm.wan1.eqdisc_opts='dual-srchost'
			set sqm.wan2=queue
			set sqm.wan2.qdisc="fq_codel"
			set sqm.wan2.script="simple.qos"
			set sqm.wan2.qdisc_advanced='0'
			set sqm.wan2.linklayer='none'
			set sqm.wan2.enabled='1'
			set sqm.wan2.interface="${wan2}"
			set sqm.wan2.download='0'
			set sqm.wan2.upload='0'
			set sqm.wan2.debug_logging='0'
			set sqm.wan2.verbosity='5'
			set sqm.wan2.iqdisc_opts='autorate-ingress dual-dsthost'
			set sqm.wan2.eqdisc_opts='dual-srchost'
			commit sqm
		EOF
		if [ "$(uci -q get network.wan3)" != "" ]; then
			wan3="$(uci -q get network.wan3.device)"
			uci -q batch <<-EOF >/dev/null
				set sqm.wan3=queue
				set sqm.wan3.qdisc="fq_codel"
				set sqm.wan3.script="simple.qos"
				set sqm.wan3.qdisc_advanced='0'
				set sqm.wan3.linklayer='none'
				set sqm.wan3.enabled='1'
				set sqm.wan3.interface="${wan3}"
				set sqm.wan3.download='0'
				set sqm.wan3.upload='0'
				set sqm.wan3.debug_logging='0'
				set sqm.wan3.verbosity='5'
				set sqm.wan3.iqdisc_opts='autorate-ingress dual-dsthost'
				set sqm.wan3.eqdisc_opts='dual-srchost'
				commit sqm
			EOF
		fi
		if [ "$(uci -q get network.wan4)" != "" ]; then
			wan4="$(uci -q get network.wan4.device)"
			uci -q batch <<-EOF >/dev/null
				set sqm.wan4=queue
				set sqm.wan4.qdisc="fq_codel"
				set sqm.wan4.script="simple.qos"
				set sqm.wan4.qdisc_advanced='0'
				set sqm.wan4.linklayer='none'
				set sqm.wan4.enabled='1'
				set sqm.wan4.interface="${wan4}"
				set sqm.wan4.download='0'
				set sqm.wan4.upload='0'
				set sqm.wan4.debug_logging='0'
				set sqm.wan4.verbosity='5'
				set sqm.wan4.iqdisc_opts='autorate-ingress dual-dsthost'
				set sqm.wan4.eqdisc_opts='dual-srchost'
				commit sqm
			EOF
		fi
	fi
fi

if [ "$(uci -q get sqm.omrvpn)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set sqm.omrvpn=queue
		set sqm.omrvpn.qdisc="cake"
		set sqm.omrvpn.script="piece_of_cake.qos"
		set sqm.omrvpn.qdisc_advanced='0'
		set sqm.omrvpn.linklayer='none'
		set sqm.omrvpn.enabled='1'
		set sqm.omrvpn.interface=tun0
		set sqm.omrvpn.download='0'
		set sqm.omrvpn.upload='0'
		set sqm.omrvpn.debug_logging='0'
		set sqm.omrvpn.verbosity='5'
		set sqm.omrvpn.overhead='40'
		set sqm.omrvpn.iqdisc_opts='autorate-ingress'
		commit sqm
	EOF
fi

sed -i 's/SQM_SYSLOG=1/SQM_SYSLOG=0/g' /etc/sqm/sqm.conf

rm -f /tmp/luci-indexcache

exit 0
